package com.aldobucchi.x.mx.core.UIComponent
{
	import mx.core.UIComponent;
	import mx.core.Container;
	
	import com.aldobucchi.x.mx.core.Container.ContainerUtil;
	
	
	/**
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class UIComponentUtil
	{
	
		
		/**
		 * 
		 * @param component
		 * @param offset
		 * @return 
		 * 
		 */		
		public static function changeDepth( component:UIComponent, offset:int ):Boolean
		{
			if ( ! canChangeDepth( component, offset ) )
				return false;
			return ContainerUtil.swapChildren( component.parent as Container, component, getSibling( component, offset ) );
		}
			
	
		/**
		 * 
		 * @param component
		 * @param offset
		 * @return 
		 * 
		 */		
		public static function canChangeDepth( component:UIComponent, offset:int ):Boolean
		{
			return getSibling( component, offset ) != null;
		}
	
	
		/**
		 * 
		 * 
		 * 
		 * @param component
		 * @param offset
		 * @return 
		 * 
		 */	
		public static function getSibling( component:UIComponent, offset:int  ):UIComponent
		{
			if ( component.parent == null ) // not parented
				return null;
				
			var index:int = component.parent.getChildIndex( component ) + offset;
			
			if ( index < 0 || index > ( component.parent.numChildren - 1 ) ) // out of bounds
				return null;		
						
			return component.parent.getChildAt( index ) as UIComponent;
		}

	
	}
	
}